const state = {
  userName: '', // 登录用户名称
  msgClientId: '', // 消息的id
  isloaded: false // 有没有从后台获取数据
}
const getters = {
  userInfo: state => {
    if (!state.isloaded) {
      let localData = sessionStorage.getItem('userInfo')
      if (localData) {
        let info = JSON.parse(localData)
        setStateValue(info)
      }
    }
    return {
      userName: state.userName,
      msgClientId: state.msgClientId
    }
  }
}

const mutations = {
  setUserInfo (state, userInfo) {
    setStateValue(userInfo)
    sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
  }
}
const actions = {
  setUserInfo: ({ commit }, userInfo) => {
    commit('setUserInfo', userInfo)
  }
}
function setStateValue (userInfo) {
  state.isloaded = true
  state.msgClientId = userInfo.msg_id
  state.userName = userInfo.user_name
}
export default {
  state,
  getters,
  actions,
  mutations
}
